from openpyxl import load_workbook
import os
import re

xlx_name = "软件1701基本信息.xlsx"
xls_path = "C:\\Users\\tonys\\OneDrive\\桌面\\软件1701acm实验报告\\"


def read_from_excel(path: str) -> dict:
    _stu_dic = {}
    wb = load_workbook(path)
    ws = wb[wb.sheetnames[0]]
    stu_list = list(ws['f'])
    no_list = list(ws['d'])
    stu_list.pop(0)
    no_list.pop(0)
    for _i, no_cell in enumerate(stu_list):
        _stu_dic[no_cell.value] = no_list[_i].value
    return _stu_dic


stu_dic = read_from_excel(xls_path + xlx_name)
path_file = os.listdir(xls_path)
unreceived_stu = stu_dic.copy()
print("\n\ntonys\\onedrive\\软件1701名单.xlsx")
print("已收到作业-------")
for filename in path_file:
    match_stu = ''
    for name in stu_dic:
        if re.search(name, filename) is not None:
            match_stu = name
            unreceived_stu.pop(match_stu)
            old_file_name = xls_path + filename
            new_file_name = xls_path + stu_dic[match_stu] + match_stu + re.search(r"\.\w+", filename, 0).group()
            os.rename(old_file_name, new_file_name)
            print(old_file_name + ">>>" + new_file_name)
            print(stu_dic[match_stu] + "  => " + match_stu + " => " + re.search(r"\.\w+", filename, 0).group())
            break
print('\n未收到,,,,,,,,,')
for stu in unreceived_stu:
    print(stu)
print("%d人没交" % len(unreceived_stu))
